//package com.sbtr.util;
//
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.UnsupportedEncodingException;
//import java.net.URLEncoder;
//import java.nio.charset.Charset;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.Locale;
//import java.util.TimeZone;
//
//import org.apache.http.client.methods.HttpPut;
//import org.apache.http.entity.FileEntity;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.entity.mime.MultipartEntityBuilder;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Component;
//import org.springframework.util.LinkedMultiValueMap;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.client.RestTemplate;
//
//import com.alibaba.fastjson.JSONObject;
//
//@Component
//public class OSSUtils2 {
//
//	@Value("${oss.config.appDomain}")
//	private String appDomain;
//
//	@Value("${oss.config.appId}")
//	private String appId;
//
//	@Value("${oss.config.appKey}")
//	private String appKey;
//
//	@Value("${oss.config.reautTimeout}")
//	private int reautTimeout;
//
//	@Value("${oss.config.bucket}")
//	private String bucket;
//
//	@Value("${oss.config.authDomain}")
//	private String authDomain;
//
//	private static final String CREATE_CERT_API = "v1/appmanager/certification/create";
//	private static final String UPLOAD_API = "/v1/oss/object/upload/";
//	private static final String PRESIGNED_URL_API = "/v1/appmanager/object/get_presignedurl";
//	public static final Logger logger = LoggerFactory.getLogger(OSSUtils2.class);
//
//	public JSONObject createCert() {
//		String url = appDomain + CREATE_CERT_API;
//		Long currentTime = System.currentTimeMillis();
//		String ts = Integer.toString(currentTime.intValue());
//		String signature = Md5Util.digest(appId + ts + appKey);
//		if (signature == null || "".equals(signature)) {
//			return null;
//		}
//		HttpHeaders headers = new HttpHeaders();
//		JSONObject postParams = new JSONObject();
//		postParams.put("appid", appId);
//		postParams.put("ts", ts);
//		postParams.put("signature", signature);
//		postParams.put("expire", reautTimeout);
//		postParams.put("acl", "wr");
//		headers.setContentType(MediaType.APPLICATION_JSON);
//		HttpEntity<JSONObject> r = new HttpEntity<>(postParams, headers);
//		RestTemplate template = new RestTemplate();
//		return template.postForObject(url, r, JSONObject.class);
//	}
//
//	public void uploadObject(String cerl, File file) throws UnsupportedEncodingException {
//		String url = appDomain + UPLOAD_API + appId + "/" + bucket + "/" + URLEncoder.encode(file.getName(), "utf-8");
//		try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
//				InputStream is = new FileInputStream(file);
//				BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
//			HttpPut httpput = new HttpPut(url);
//			TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
//			Date time = new Date();
//			SimpleDateFormat sdf = new SimpleDateFormat("E", Locale.ENGLISH);
//			String week = sdf.format(time);
//			// 璁剧疆header
//			httpput.setHeader("Content-type", "application/json");
//			httpput.setHeader("x-amz-date", week + "," + time.toGMTString());
//			httpput.setHeader("Certification", cerl);
//			StringBuilder sb = new StringBuilder();
//			String data;
//			while ((data = br.readLine()) != null) {
//				sb.append(data + "\r\n");
//			}
//			// 缁勭粐璇锋眰鍙傛暟
//			StringEntity stringEntity = new StringEntity(sb.toString(), "utf-8");
//			httpput.setEntity(stringEntity);
//			// 鍝嶅簲淇℃伅
//			closeableHttpClient.execute(httpput);
//		} catch (Exception e) {
//			logger.error("uploadObject error:{}", e);
//		}
//	}
//
//	public JSONObject uploadFile(File file, String cert) throws IOException {
//		String url = appDomain + UPLOAD_API + appId + "/" + bucket + "/" + URLEncoder.encode(file.getName(), "utf-8");
//		HttpPut httpPut = new HttpPut(url);
//		TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
//		Date time = new Date();
//		SimpleDateFormat sdf = new SimpleDateFormat("E", Locale.ENGLISH);
//		String week = sdf.format(time);
//		httpPut.addHeader("x-amz-date", week + "," + time.toGMTString());
//		httpPut.addHeader("x-amz-acl", "public-read-write");
//		httpPut.addHeader("Certification", cert);
//		MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create()
//				.setCharset(Charset.forName("UTF-8"));
//		multipartEntityBuilder.addBinaryBody("data", file);
//		org.apache.http.HttpEntity entiy = multipartEntityBuilder.build();
//		httpPut.setEntity(entiy);
//		try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault();) {
//			closeableHttpClient.execute(httpPut);
//		}
//		return null;
//	}
//
//	public JSONObject init(String file, String cert) {
//		String url = appDomain + "/v1/appmanager/multiupload/init";
//		HttpHeaders headers = new HttpHeaders();
//		JSONObject postParams = new JSONObject();
//		postParams.put("appid", appId);
//		postParams.put("bucket", bucket);
//		postParams.put("object", file);
//		postParams.put("certification", cert);
//		headers.setContentType(MediaType.APPLICATION_JSON);
//		HttpEntity<JSONObject> r = new HttpEntity<>(postParams, headers);
//		RestTemplate template = new RestTemplate();
//		return template.postForObject(url, r, JSONObject.class);
//	}
//
//	public JSONObject list(String file, String cert, String uploadid) {
//		String url = appDomain + "/v1/appmanager/multiupload/list";
//		HttpHeaders headers = new HttpHeaders();
//		JSONObject postParams = new JSONObject();
//		postParams.put("appid", appId);
//		postParams.put("bucket", bucket);
//		postParams.put("object", file);
//		postParams.put("certification", cert);
//		postParams.put("uploadid", uploadid);
//		headers.setContentType(MediaType.APPLICATION_JSON);
//		HttpEntity<JSONObject> r = new HttpEntity<>(postParams, headers);
//		RestTemplate template = new RestTemplate();
//		return template.postForObject(url, r, JSONObject.class);
//
//	}
//
//	public JSONObject complete(String fileName, String cert, String uploadid,
//			com.alibaba.fastjson.JSONArray jsonArray) {
//		String url = appDomain + "/v1/appmanager/multiupload/complete";
//		HttpHeaders headers = new HttpHeaders();
//		JSONObject postParams = new JSONObject();
//		postParams.put("appid", appId);
//		postParams.put("bucket", bucket);
//		postParams.put("object", fileName);
//		postParams.put("certification", cert);
//		postParams.put("uploadid", uploadid);
//		postParams.put("parts", jsonArray);
//		headers.setContentType(MediaType.APPLICATION_JSON);
//		HttpEntity<JSONObject> r = new HttpEntity<>(postParams, headers);
//		RestTemplate template = new RestTemplate();
//		return template.postForObject(url, r, JSONObject.class);
//	}
//
//	public JSONObject getPresignedUrl(String cert, String fileName, int expire) {
//		if (cert == null || "".equals(cert)) {
//			return null;
//		}
//		String url = appDomain + PRESIGNED_URL_API;
//		MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
//		postParameters.add("appid", appId);
//		postParameters.add("bucket", bucket);
//		postParameters.add("object", fileName);
//		postParameters.add("acl", "public-read-write");
//		postParameters.add("expire", expire);
//		postParameters.add("certification", cert);
//		HttpHeaders headers = new HttpHeaders();
//		headers.add("Content-Type", "application/x-www-form-urlencoded;");
//		HttpEntity<MultiValueMap<String, Object>> r = new HttpEntity<>(postParameters, headers);
//		RestTemplate template = new RestTemplate();
//		return template.postForObject(url, r, JSONObject.class);
//	}
//
//	public JSONObject uploadFilePart(File file, String fileName, String cert, int partNumber, String uploadId)
//			throws UnsupportedEncodingException {
//		String url = authDomain + "/v1/oss/object/uploadpart" + "/" + appId + "/" + bucket + "/"
//				+ URLEncoder.encode(fileName, "utf-8") + "?partNumber=" + partNumber + "&uploadId=" + uploadId;
//		try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault();) {
//			HttpPut httpput = new HttpPut(url);
//			TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
//			Date time = new Date();
//			SimpleDateFormat sdf = new SimpleDateFormat("E", Locale.ENGLISH);
//			String week = sdf.format(time);
//			// 璁剧疆header
//			httpput.setHeader("Content-type", "application/ms-excel");
//			httpput.setHeader("x-amz-date", week + "," + time.toGMTString());
//			httpput.setHeader("Certification", cert);
//			FileEntity entity = new FileEntity(file, "text/plain; charset=\"UTF-8\"");
//			httpput.setEntity(entity);
//			closeableHttpClient.execute(httpput);
//		} catch (Exception e) {
//			logger.error("uploadFilePart error:{}", e);
//		}
//		return null;
//	}
//
//	public static org.springframework.http.ResponseEntity<String> post() {
//		String url = "http://gaea.meicloud.com/k8s/v2/app/stop";
//		RestTemplate template = new RestTemplate();
//		MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
//		paramMap.add("operate", "stop");
//		paramMap.add("app", "{'cluster_id':9,'id':1629}");
//		HttpHeaders headers = new HttpHeaders();
//		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//		headers.add("Cookie", "");
//		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, headers);
//		return template.postForEntity(url, httpEntity, String.class);
//	}
//}
